
function getAllShopCartMsg(userid){
	$.post("/CuitShopPlatform/servlet/ShopCartController",{"action":"getAllShopCartMsg","userid":userid}, function(data){
		if(data[0]==null){
			var node="<div class='col-md-12 column thisShop'><a href='index.html' style='color:red;font-size:15px;font-weight:bold'>当前购物车为空,点击返回首页</a></div>"
			$(".tolPrice").text("￥0");
			$(node).appendTo(".cartContent");
		}
		var tolPrice=0;
		for(var i in data){
			tolPrice=parseFloat(data[i].price)*parseInt(data[i].num)+tolPrice;
			var node='<div class="col-md-12 column thisShop" id='+data[i].carid+'><div class="col-md-2 column shopImg"><img src="'+data[i].picture+'" alt=""></div>'
                                               +'<div class="col-md-2 column shopName" >'+data[i].shopname+'</div>'
        				      +'<div class="col-md-2 column shopSku">34*78cm</div><div class="col-md-2 column chgNum">'
          				       +'<span class="sub">-</span><input type="text" class="Num" value="'+data[i].num+'"><span class="add">+</span>'
        				       +'</div><div class="col-md-2 column price">￥'+data[i].price+'</div><div class="col-md-2 column action del" >删除</div></div>';
        				       $(node).appendTo(".cartContent");
        				   };
        				   $(".tolPrice").text("￥"+tolPrice);
	},"json");
};

//all buy
function pay(userid,price){
	$.post("/CuitShopPlatform/servlet/ShopCartController",{"action":"pay","userid":userid,"price":price},function(data){
		if(data.flag){
			alert("购买成功!");
			window.location.href="user.html";
		}
		else{
			alert("购买失败");
			window.location.reload();
		}
	},"json");
}

$(function(){
	var userid;
	//function isLogin(){
		$.post("/CuitShopPlatform/servlet/isLoginController",{"action":"isLogin"}, function(data){
			if(data.loginflag=='0'){
				alert("请先登录");
				window.location.href="login.html";
			}
			$(".rightTopBar").empty();
			var node="<a class='userMsg' href='user.html' id="+data.userid+">"+"欢迎-"+data.username+"</a>";
			$(node).appendTo(".rightTopBar");
			userid=parseInt($(".userMsg").attr("id"));
			console.log(userid);
			getAllShopCartMsg(userid);
		},"json");
	//};

	
	//del function
	$(document).on("click",".del",function(){
		var carid=parseInt($(this).parents(".thisShop").attr("id"));
		$.post("/CuitShopPlatform/servlet/ShopCartController",{"action":"delThisShop","carid":carid},function(data){
			if(data.flag){
				alert("删除成功");
				window.location.reload();
			}
			else{
				alert("删除失败");
				window.location.reload();
			}
		})
	});
	
	//add function
	$(document).on("click",".add", function(){
		var carid=parseInt($(this).parents(".thisShop").attr("id"));
		var num=parseInt($(this).siblings(".Num").val());
		num++;
		$(this).siblings(".Num").val(num);
		var tolPriceNow=parseFloat($(".tolPrice").text().replace(/[^0-9]/ig,""))+parseFloat($(this).parent(".chgNum").siblings(".price").text().replace(/[^0-9]/ig,""));
		$(".tolPrice").text("￥"+tolPriceNow);
		$.post("/CuitShopPlatform/servlet/ShopCartController",{"action":"add","carid":carid,"num":num},function(data){
			//window.location.reload();
		},"json");

	});

	//sub function
	$(document).on("click",".sub",function(){
		var carid=parseInt($(this).parents(".thisShop").attr("id"));
		var num=parseInt($(this).siblings(".Num").val());
		if(num==1)
			return;
		num--;
		$(this).siblings(".Num").val(num);
		var tolPriceNow=parseFloat($(".tolPrice").text().replace(/[^0-9]/ig,""))-parseFloat($(this).parent(".chgNum").siblings(".price").text().replace(/[^0-9]/ig,""));
		$(".tolPrice").text("￥"+tolPriceNow);
		$.post("/CuitShopPlatform/servlet/ShopCartController",{"action":"sub","carid":carid,"num":num},function(data){
			//window.location.reload();
		},"json");
		sub(carid,num);
	});

	//buy function
	$(document).on("click",".buy",function(){
		//is not safe
		var price=parseFloat($(".tolPrice").text().replace(/[^0-9]/ig,""));
		if (price==0) {
			alert("请先添加商品！");
			return false;
		}
		pay(userid,price);
	});

});